﻿package org.openChart.meta.manager 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filters.GradientGlowFilter;
	import flash.net.URLLoader;
	import flash.net.URLRequest
	import flash.text.StyleSheet;
	import org.openChart.meta.Axis;
	import org.openChart.meta.BaseChart;
	import org.openChart.meta.data.GroupSet;
	import org.openChart.meta.Stick;
	import org.openChart.meta.StickLine;
	import org.openChart.meta.Style;
	import org.openChart.meta.TextLabel;
	import org.openChart.meta.XAxis;
	import org.openChart.meta.YAxis;
	import org.openChart.meta.ChartType;

	/**
	 * Load the chart defination file and build up the chat meta data model.
	 * 
	 * @author Chongyuan
	 */
	public class XmlDefinationLoader extends XmlBase
	{
		private var xml:XML ;
		private var chart_:BaseChart;
		
		private var file:String = "chart_define/drusy-bar-chart-2d.xml";
		//private var file:String = "chart_define/cumulate-bar-chart-2d.xml";
		//private var file:String = "chart_define/percent-bar-chart-2d.xml";
		
		//private var file:String = "chart_define/drusy-bar-chart-3d-cube.xml";
		//private var file:String = "chart_define/cumulate-bar-chart-3d-cube.xml";
		//private var file:String = "chart_define/percent-bar-chart-3d-cube.xml";
		
		//private var file:String = "chart_define/drusy-bar-chart-3d-cylinder.xml";
		//private var file:String = "chart_define/cumulate-bar-chart-3d-cylinder.xml";
		//private var file:String = "chart_define/percent-bar-chart-3d-cylinder.xml";
		
		//private var file:String = "chart_define/drusy-bar-chart-3d-cone.xml";
		//private var file:String = "chart_define/cumulate-bar-chart-3d-cone.xml";
		//private var file:String = "chart_define/percent-bar-chart-3d-cone.xml";
		
		//private var file:String = "chart_define/drusy-bar-chart-3d-pyramid.xml";
		//private var file:String = "chart_define/cumulate-bar-chart-3d-pyramid.xml";
		//private var file:String = "chart_define/percent-bar-chart-3d-pyramid.xml";
		
		//private var file:String = "chart_define/percent-line-chart.xml";
		//private var file:String = "chart_define/cumulate-line-chart.xml";
		//private var file:String = "chart_define/drusy-line-chart.xml";
		
		//private var file:String = "chart_define/disconnected-pie-chart-2d.xml";
		//private var file:String = "chart_define/disconnected-pie-chart-3d.xml";
		//private var file:String = "chart_define/pie-chart-2d.xml";
		//private var file:String = "chart_define/pie-chart-3d.xml";
		
		public function XmlDefinationLoader( fileName:String = null ) 
		{
			if (fileName)
				this.file = fileName;
		}
		
		/**
		 * Load the chart defination file.
		 */
		public function loadXml() :void
		{

			var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, xmlLoaded);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            var request:URLRequest = new URLRequest(file);
            try {
                loader.load(request);
            } catch (error:Error) {
				exception();
            }
			
		}
		
		/**
		 * The chart defination is successfully loaded .
		 * @param	event
		 */
		private function xmlLoaded(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
			var xmlString:String = new String(loader.data);
			xml = new XML(loader.data);
			var chartType:String;
			//trace(xml.coordinate);
			if (new String(xml.chart) != "")
			{//trace(xml.pie);
				for each ( var coorXml:XML in xml.chart[0])
				{
					chartType = new String(coorXml.@type);
					if (chartType == ChartType.DRUSY_BAR_CHART_2D
						|| chartType == ChartType.PERCENT_BAR_CHART_2D
						|| chartType == ChartType.CUMULATE_BAR_CHART_2D
						|| chartType == ChartType.CUMULATE_LINE_CHART
						|| chartType == ChartType.PERCENT_LINE_CHART
						|| chartType == ChartType.DRUSY_LINE_CHART
						|| chartType == ChartType.DRUSY_BAR_CHART_3D_CUBE
						|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CUBE
						|| chartType == ChartType.PERCENT_BAR_CHART_3D_CUBE
						|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CYLINDER
						|| chartType == ChartType.DRUSY_BAR_CHART_3D_CYLINDER
						|| chartType == ChartType.PERCENT_BAR_CHART_3D_CYLINDER
						|| chartType == ChartType.DRUSY_BAR_CHART_3D_CONE
						|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CONE
						|| chartType == ChartType.PERCENT_BAR_CHART_3D_CONE
						|| chartType == ChartType.DRUSY_BAR_CHART_3D_PYRAMID
						|| chartType == ChartType.CUMULATE_BAR_CHART_3D_PYRAMID
						|| chartType == ChartType.PERCENT_BAR_CHART_3D_PYRAMID)
					{
						//trace(coorXml);
						var barChartBuilder:BarChartBuilder = new BarChartBuilder();
						chart_ = barChartBuilder.buildBarChart(coorXml, chartType);
						chart.chartType = chartType;
					}
					
				}
			}
			else if (new String(xml.pie) != "")
			{
				//trace(xml.pie);
				for each ( var pieXml:XML in xml.pie)
				{
					//trace(pieXml);
					chartType = new String(pieXml.@type);
					if (chartType == ChartType.PIE_CHART_2D
						|| chartType == ChartType.PIE_CHART_3D
						|| chartType == ChartType.DISCONNECTED_PIE_CHART_2D
						|| chartType == ChartType.DISCONNECTED_PIE_CHART_3D)
					{
						var pieChartBuilder:PieChartBuilder = new PieChartBuilder();
						chart_ = pieChartBuilder.buildPieChart(pieXml, chartType);
						chart.chartType = chartType;
						
					}
					
				}
			}
			dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
        }
		
		
		
        private function ioErrorHandler(event:IOErrorEvent):void {
			dispatchEvent(new LoaderEvent(LoaderEvent.FAILED));
        }
		
		private function exception():void
		{
			dispatchEvent(new LoaderEvent(LoaderEvent.EXCEPTION) );
		}
		
		public function get chart():BaseChart
		{
			if (chart_)
			{
				return chart_;
			}
			else
			{
				throw new Error("The chart is null");
			}
		}
	}
	
}